import cx_Oracle
#数据库操作

def updateData(DEVICECODE,PDMTECHCODE):

    # 连接配置
    dsn = cx_Oracle.makedsn(
        host="192.168.16.37",
        port=49161,
        sid="XE"  # 或 sid="XE"（根据数据库类型选择）
    )

    # 建立连接
    conn = cx_Oracle.connect(
        user="SANMENADMIN",
        password="qwe123",
        dsn=dsn
    )

    try:
        # 创建游标
        cursor = conn.cursor()

        # 查询任务编号
        selectSql = "SELECT * FROM (SELECT * FROM PMPT_PDMTASK where DEVICECODE = '%s' AND PDMTECHCODE = '%s' ORDER BY TASKCODE DESC) WHERE ROWNUM = 1" % (DEVICECODE,PDMTECHCODE)
        cursor.execute(selectSql)
        results = cursor.fetchall()
        if not results:
            print("未找到对应记录")
            return


        TASKCODE = results[0][0]

        # 更新数据
        updateSql = "UPDATE PMPT_PDMTASK SET STATUS = 'Finished' , EVALUATEDSTATUS = 'Acceptable' where  TASKCODE = '%s'" % (TASKCODE)
        cursor.execute(updateSql)
        # 提交事务
        conn.commit()
        print(f"更新成功，影响行数：{cursor.rowcount}")

    except cx_Oracle.DatabaseError as e:
        # 错误处理
        error, = e.args
        print(f"Oracle 错误 {error.code}: {error.message}")
        conn.rollback()

    finally:
        # 关闭连接
        cursor.close()
        conn.close()

#
# if __name__ == '__main__':
#     updateData("2-VWS-ES-MS01A01","UT")